home *** CD-ROM | disk | FTP | other *** search
/ Greenhouse Effect Detection Expriment / NASA Greenhouse Effect Detection Expriment 1992 - Disc 2.iso / software / dos / cdf22pc / src / tools / cdfcvt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-04  |  29.2 KB  |  982 lines

  1. /******************************************************************************
  2. *
  3. *  NSSDC/CDF                            CDFconvert.
  4. *
  5. *  Version 2.0, 4-Mar-92, ST Systems (STX)
  6. *
  7. *  Modification history:
  8. *
  9. *   V1.0  24-Jan-91, H Leckner    Original version (for CDF V2.0).
  10. *   V2.0   4-Mar-92, J Love    IBM PC port (major changes).
  11. *
  12. ******************************************************************************/
  13.  
  14. #include "cdfdist.h"
  15. #include "cdfcvt.h"
  16.  
  17. /******************************************************************************
  18. * Macro constants.
  19. ******************************************************************************/
  20.  
  21. #define SOURCEformat    (-1)
  22. #define SOURCEmajority    (-1)
  23. #define SOURCEencoding    (-1)
  24.  
  25. /******************************************************************************
  26. * Global variables.
  27. ******************************************************************************/
  28.  
  29. Boolean useSkeletonCDF;
  30. Boolean mLog;
  31. long dstFormat = SOURCEformat;
  32. long dstMajority = SOURCEmajority;
  33. long dstEncoding = SOURCEencoding;
  34. long numDims, dimSizes[CDF_MAX_DIMS];
  35. long srcFormat, srcMajority, srcEncoding;
  36. CDFid srcId, dstId, sktId;
  37.  
  38. /******************************************************************************
  39. * Online help text.
  40. ******************************************************************************/
  41.  
  42. static char *instructions[] = {
  43. #if defined(vms)
  44. "Usage:         $ CDFCONVERT [/SKELETON=<cdf-path>] [/[NO]LOG]",
  45. "                            <src-cdf-spec> <dst-cdf-spec>",
  46. "                            [/SINGLE or /MULTI] [/ROW or /COLUMN]",
  47. "                            [/HOST or /NETWORK]",
  48. #endif
  49. #if defined(unix)
  50. "Usage:         % cdfconvert [-skeleton <cdf-path>] [-[no]log]",
  51. "                            <src-cdf-spec> <dst-cdf-spec>",
  52. "                            [-single or -multi] [-row or -column]",
  53. "                            [-host or -network]",
  54. #endif
  55. #if defined(__MSDOS__)
  56. "Usage:         > cdfconvert [-skeleton <cdf-path>] [-[no]log]",
  57. "                            <src-cdf-spec> <dst-cdf-spec>",
  58. "                            [-single or -multi] [-row or -column]",
  59. "                            [-host or -network]",
  60. #endif
  61. "",
  62. "Purpose:       CDFconvert is used to convert one or more CDFs.  Conversions",
  63. "               can be from older releases of CDF (eg. Version 1 CDFs) to the",
  64. "               current release and/or to change the format, majority, or",
  65. "               encoding.",
  66. "",
  67. "Parameter(s):  <src-cdf-spec>",
  68. "                  The source CDF(s).  This can be either a single CDF",
  69. "                  pathname or a directory/wildcard path in which case all",
  70. "                  CDFs which match the specification will be converted.",
  71. "                  Wildcards are allowed in the CDF name but not in the",
  72. "                  directory path.  In either case, do not specify an",
  73. "                  extension.",
  74. "",
  75. "               <dst-cdf-spec>",
  76. "                  The destination of the converted CDF(s).  This may be a",
  77. "                  single CDF pathname only if {\tt <src-cdf-spec>} is a",
  78. "                  single CDF pathname.  If the directory paths are the",
  79. "                  same, then a different CDF name must be specified.  If",
  80. "                  more than one CDF is being converted, this must be a",
  81. "                  directory path (other than the source directory path).",
  82. "                  This may also be a directory path if only one CDF",
  83. "                  is being converted.  In any case, do not specify an",
  84. "                  extension.",
  85. "",
  86. #if defined(vms)
  87. "Qualifier(s):  /SKELETON=<cdf-path>",
  88. #endif
  89. #if defined(unix) | defined(__MSDOS__)
  90. "Qualifier(s):  -skeleton <cdf-path>",
  91. #endif
  92. "                  <cdf-path> is the pathname of a skeleton CDF to be used",
  93. "                  during the conversions (do not enter an extension).  The",
  94. "                  skeleton CDF is used in the following cases.",
  95. "",
  96. "                    1. If an assumed attribute scope is found in a source",
  97. "                       CDF, then the scope of that attribute in the skeleton",
  98. "                       CDF (if the attribute exists) will be used in the",
  99. "                       destination CDF.",
  100. "",
  101. "                    2. If a format for the destination CDF(s) was not",
  102. "                       specified, then the format of the skeleton CDF will",
  103. "                       be used.",
  104. "",
  105. "                    3. If a majority for the destination CDF(s) was not",
  106. "                       specified, then the majority of the skeleton CDF will",
  107. "                       be used.",
  108. "",
  109. "                    4. If an encoding for the destination CDF(s) was not",
  110. "                       specified, then the encoding of the skeleton CDF will",
  111. "                       be used.",
  112. "",
  113. "                  Specifying a skeleton CDF is optional.",
  114. "",
  115. #if defined(vms)
  116. "               /[NO]LOG",
  117. #endif
  118. #if defined(unix) | defined(__MSDOS__)
  119. "               -[no]log",
  120. #endif
  121. "                  Specifies whether or not messages are displayed",
  122. "                  indicating the progress of a conversion.  The",
  123. "                  default is logging disabled.",
  124. "",
  125. #if defined(vms)
  126. "               /SINGLE",
  127. "               /MULTI",
  128. #endif
  129. #if defined(unix) | defined(__MSDOS__)
  130. "               -single",
  131. "               -multi",
  132. #endif
  133. "                  The format of the destination CDF(s).  This overrides the",
  134. "                  format of the skeleton CDF (if specified).  If neither",
  135. "                  this qualifier nor a skeleton CDF is specified, then the",
  136. "                  format of the destination CDF(s) will be the same as that",
  137. "                  of the source CDF(s).  (Optional).",
  138. "",
  139. #if defined(vms)
  140. "               /ROW",
  141. "               /COLUMN",
  142. #endif
  143. #if defined(unix) | defined(__MSDOS__)
  144. "               -row",
  145. "               -column",
  146. #endif
  147. "                  The majority of the destination CDF(s).  This overrides",
  148. "                  the majority of the skeleton CDF (if specified).  If",
  149. "                  neither this qualifier nor a skeleton CDF is specified,",
  150. "                  then the majority of the destination CDF(s) will be the",
  151. "                  same as that of the source CDF(s).  (Optional).",
  152. "",
  153. #if defined(vms)
  154. "               /HOST",
  155. "               /NETWORK",
  156. #endif
  157. #if defined(unix) | defined(__MSDOS__)
  158. "               -host",
  159. "               -network",
  160. #endif
  161. "                  The encoding of the destination CDF(s).  This overrides",
  162. "                  the encoding of the skeleton CDF (if specified).  If",
  163. "                  neither this qualifier nor a skeleton CDF is specified,",
  164. "                  then the encoding of the destination CDF(s) will be the",
  165. "                  same as that of the source CDF(s).  (Optional).",
  166. "",
  167. #if defined(vms)
  168. "Example(s):    $ CDFCONVERT CDF$SMPL:TPLATE0 TEMPLATE0X",
  169. "               $ CDFCONVERT CDF$SMPL: USER_DISK:[USER.CDF]",
  170. "               $ CDFCONVERT CAC_SST_BLENDED CAC_SST_BLENDEDX/SINGLE/NETWORK",
  171. "               $ CDFCONVERT/SKELETON=CDF$SMPL:TPLATE3 CAC_SST_* [USER.CDF]",
  172. #endif
  173. #if defined(unix)
  174. "Example(s):    % cdfconvert ../samples/tplate0 template0x",
  175. "               % cdfconvert ../samples/ /disk4/user/cdf/",
  176. "               % cdfconvert cac_sst_blended cac_sst_1 -single -network",
  177. "               % cdfconvert -skeleton tplate3 '../cdf/cac_sst_*' ~user/cdf",
  178. #endif
  179. #if defined(__MSDOS__)
  180. "Example(s):    > cdfconvert ..\\samples\\tplate0 tplate0x",
  181. "               > cdfconvert ..\\samples c:\\disk4\\user\\cdf",
  182. "               > cdfconvert cac_sst cac_sst1 -single -network",
  183. "               > cdfconvert -skeleton tplate3 ..\\cac_sst* b:\\user\\cdf",
  184. #endif
  185. NULL };
  186.  
  187. /******************************************************************************
  188. * main.
  189. ******************************************************************************/
  190.  
  191. #if defined(vms)
  192. main (argc, argv)
  193. #else
  194. void main (argc, argv)
  195. #endif
  196. int        argc;
  197. char        *argv[];
  198. {
  199. QOP *qop;
  200. static char *validQuals[] = { "single", "multi", "row", "column",
  201.                   "host", "network", "skeleton",
  202.                   "log", "nolog", NULL };
  203. static int optRequired[] = { FALSE, FALSE, FALSE, FALSE,
  204.                  FALSE, FALSE, TRUE,
  205.                  FALSE, FALSE, 0 };
  206. CDFstatus status;
  207. char tempSpec[CDF_PATHNAME_LEN + 1];
  208. char srcSpec[CDF_PATHNAME_LEN + 1];
  209. char dstSpec[CDF_PATHNAME_LEN + 1];
  210. char srcPath[CDF_PATHNAME_LEN + 1];
  211. char dstPath[CDF_PATHNAME_LEN + 1];
  212. char sktPath[CDF_PATHNAME_LEN + 1];
  213. int count;
  214. long numCDFs;
  215. int i;
  216. char **directories;
  217. char **CDFnames;
  218.  
  219. /******************************************************************************
  220. * Get qualifiers/options/parameters.
  221. ******************************************************************************/
  222.  
  223. switch (argc) {
  224.   case 1:
  225.     PageInst (instructions);
  226.     Exit;
  227.   default:
  228.     qop = Qop (argc, argv, validQuals, optRequired);
  229.     if (qop == NULL) ExitBAD;
  230.  
  231.     switch (qop->Nparms) {
  232.       case 0:
  233.       case 1:
  234.     printf ("Missing parameter(s).\n");
  235.     ExitBAD;
  236.       case 2:
  237.     strcpy (srcSpec, qop->parms[0]);
  238.     strcpy (dstSpec, qop->parms[1]);
  239. #if defined(vms) | defined(__MSDOS__)
  240.     Upcase (srcSpec);
  241.     Upcase (dstSpec);
  242. #endif
  243.     break;
  244.       default:
  245.     printf ("Too many parameters.\n");
  246.     ExitBAD;
  247.     }
  248.  
  249.     /**************************************************************************
  250.     * Open/inquire skeleton CDF before...
  251.     **************************************************************************/
  252.  
  253.     if (qop->qualEntered[6]) {
  254.       strcpy (sktPath, qop->qualOpt[6]);
  255.       printf ("Opening Skeleton CDF: %s\n", sktPath);
  256.       status = CDFlib (OPEN_, CDF_, sktPath, &sktId,
  257.                GET_, CDF_FORMAT_, &dstFormat,
  258.                  CDF_MAJORITY_, &dstMajority,
  259.                  CDF_ENCODING_, &dstEncoding,
  260.                NULL_);
  261.       if (status < CDF_OK) {
  262.     StatusHandler ("SKT", status);
  263.     useSkeletonCDF = FALSE;
  264.       }
  265.       else
  266.     useSkeletonCDF = TRUE;
  267.     }
  268.     else
  269.       useSkeletonCDF = FALSE;
  270.  
  271.     /**************************************************************************
  272.     * ...checking for an overriding format, majority, or encoding.
  273.     **************************************************************************/
  274.  
  275.     count = 0;
  276.     if (qop->qualEntered[0]) count++;
  277.     if (qop->qualEntered[1]) count++;
  278.     switch (count) {
  279.       case 0:
  280.     break;        /* no override */
  281.       case 1:
  282.     if (qop->qualEntered[0])
  283.       dstFormat = SINGLE_FILE;
  284.     else
  285.       dstFormat = MULTI_FILE;
  286.     break;
  287.       default:
  288.     printf ("Conflicting qualifiers\n");
  289.     ExitBAD;
  290.     }
  291.  
  292.     count = 0;
  293.     if (qop->qualEntered[2]) count++;
  294.     if (qop->qualEntered[3]) count++;
  295.     switch (count) {
  296.       case 0:
  297.     break;        /* no override */
  298.       case 1:
  299.     if (qop->qualEntered[2])
  300.       dstMajority = ROW_MAJOR;
  301.     else
  302.       dstMajority = COL_MAJOR;
  303.     break;
  304.       default:
  305.     printf ("Conflicting qualifiers\n");
  306.     ExitBAD;
  307.     }
  308.  
  309.     count = 0;
  310.     if (qop->qualEntered[4]) count++;
  311.     if (qop->qualEntered[5]) count++;
  312.     switch (count) {
  313.       case 0:
  314.     break;        /* no override */
  315.       case 1:
  316.     if (qop->qualEntered[4])
  317.       dstEncoding = HOST_ENCODING;
  318.     else
  319.       dstEncoding = NETWORK_ENCODING;
  320.     break;
  321.       default:
  322.     printf ("Conflicting qualifiers\n");
  323.     ExitBAD;
  324.     }
  325.  
  326.     count = 0;
  327.     if (qop->qualEntered[7]) count++;
  328.     if (qop->qualEntered[8]) count++;
  329.     switch (count) {
  330.       case 0:
  331.     mLog = FALSE;
  332.     break;
  333.       case 1:
  334.     if (qop->qualEntered[7])
  335.       mLog = TRUE;
  336.     else
  337.       mLog = FALSE;
  338.     break;
  339.       default:
  340.     printf ("Conflicting qualifiers\n");
  341.     ExitBAD;
  342.     }
  343.  
  344.     break;
  345. }
  346.  
  347. /******************************************************************************
  348. * Determine CDF(s) to convert.
  349. ******************************************************************************/
  350.  
  351. strcpy (tempSpec, srcSpec);
  352. strcat (tempSpec, ".cdf");
  353.  
  354. if ((! IsReg(tempSpec)) && (! IsDir(dstSpec))) {
  355.   printf
  356.      ("Source directory/wildcard specified, destination must be directory.\n");
  357.   ExitBAD;
  358. }
  359.  
  360. if (IsDir(srcSpec))
  361.   AppendToDir (srcSpec, "*.cdf");
  362. else
  363.   strcat (srcSpec, ".cdf");
  364.  
  365. numCDFs = DirList (srcSpec, &directories, &CDFnames);
  366. if (numCDFs < 1) {
  367.   fprintf(stderr, "No CDF(s) found in source directory.\n");
  368.   ExitBAD;
  369. }
  370.  
  371. RemoveExtensions (numCDFs, CDFnames);
  372.  
  373. /******************************************************************************
  374. * Convert CDFs.
  375. ******************************************************************************/
  376.  
  377. for (i = 0; i < numCDFs; i++) {
  378.    /***************************************************************************
  379.    * Build paths to open/display.
  380.    ***************************************************************************/
  381.  
  382.    strcpy (srcPath, directories[i]);
  383.    AppendToDir (srcPath, CDFnames[i]);
  384.  
  385.    strcpy (dstPath, dstSpec);
  386.    if (IsDir(dstPath)) AppendToDir (dstPath, CDFnames[i]);
  387.  
  388.    ConvertCDF (srcPath, dstPath);
  389. }
  390.  
  391. /******************************************************************************
  392. * Close skeleton CDF if one was used.
  393. ******************************************************************************/
  394.  
  395. if (useSkeletonCDF) {
  396.   status = CDFclose (sktId);
  397.   StatusHandler ("STK", status);            /* Ignore return. */
  398. }
  399.  
  400. Exit;
  401. }
  402.  
  403. /******************************************************************************
  404. * ConvertCDF.
  405. ******************************************************************************/
  406.  
  407. Boolean ConvertCDF (srcPath, dstPath)
  408. char *srcPath;
  409. char *dstPath;
  410. {
  411. CDFstatus status;
  412. long format, majority, encoding;
  413.  
  414. printf ("Converting \"%s\" to \"%s\"\n", srcPath, dstPath);
  415.  
  416. /***************************************************************************
  417. * Open source CDF.  If an error occurs, skip to next CDF.
  418. ***************************************************************************/
  419.  
  420. status = CDFlib (OPEN_, CDF_, srcPath, &srcId,
  421.          GET_, CDF_FORMAT_, &srcFormat,
  422.                CDF_MAJORITY_, &srcMajority,
  423.                CDF_ENCODING_, &srcEncoding,
  424.                CDF_NUMDIMS_, &numDims,
  425.                CDF_DIMSIZES_, dimSizes,
  426.          NULL_);
  427. if (! StatusHandler("SRC",status)) return FALSE;
  428.  
  429. /***************************************************************************
  430. * Determine format, majority, and encoding of destination CDF.
  431. ***************************************************************************/
  432.  
  433. if (dstFormat == SOURCEformat)
  434.   format = srcFormat;
  435. else
  436.   format = dstFormat;
  437.  
  438. if (dstMajority == SOURCEmajority)
  439.   majority = srcMajority;
  440. else
  441.   majority = dstMajority;
  442.  
  443. if (dstEncoding == SOURCEencoding)
  444.   encoding = srcEncoding;
  445. else
  446.   encoding = dstEncoding;
  447.  
  448. /***************************************************************************
  449. * Create destination CDF.  If an error occurs, close source CDF and return.
  450. ***************************************************************************/
  451.  
  452. status = CDFlib (CREATE_, CDF_, dstPath, numDims, dimSizes, &dstId,
  453.          PUT_, CDF_FORMAT_, format,
  454.                CDF_MAJORITY_, majority,
  455.                CDF_ENCODING_, encoding,
  456.          NULL_);
  457. if (! StatusHandler("DST",status)) {
  458.   status = CDFclose (srcId);
  459.   StatusHandler ("SRC", status);
  460.   return FALSE;
  461. }
  462.  
  463. /***************************************************************************
  464. * Convert attributes.  If that is successful, convert variables.
  465. ***************************************************************************/
  466.  
  467. if (! ConvertAttributes()) {
  468.   status = CDFclose (srcId);
  469.   StatusHandler ("SRC", status);
  470.   status = CDFclose (dstId);
  471.   StatusHandler ("DST", status);
  472.   return FALSE;
  473. }
  474.  
  475. /***************************************************************************
  476. * Convert variables.
  477. ***************************************************************************/
  478.  
  479. if (! ConvertVariables (srcMajority, majority)) {
  480.   status = CDFclose (srcId);
  481.   StatusHandler ("SRC", status);
  482.   status = CDFclose (dstId);
  483.   StatusHandler ("DST", status);
  484.   return FALSE;
  485. }
  486.  
  487. /***************************************************************************
  488. * Close source and destination CDFs.
  489. ***************************************************************************/
  490.  
  491. status = CDFclose (srcId);
  492. StatusHandler ("SRC", status);            /* Ignore return. */
  493.  
  494. status = CDFclose (dstId);
  495. StatusHandler ("DST", status);            /* Ignore return. */
  496.  
  497. /*****************************************************************************/
  498.  
  499. return TRUE;    /* Conversion successful. */
  500. }
  501.  
  502. /******************************************************************************
  503. * ConvertAttributes.
  504. ******************************************************************************/
  505.  
  506. Boolean ConvertAttributes ()
  507. {
  508. CDFstatus status;
  509. long attrNum, entryNum, ignoredNum;
  510. long numAttrs, maxEntry;
  511. long scope, dataType, numElements;
  512. long sktScope;
  513. char attrName[CDF_ATTR_NAME_LEN + 1];
  514. void *value;
  515.  
  516. if (mLog) printf ("  (converting attributes)\n");
  517.  
  518. /******************************************************************************
  519. * Inquire number of attributes to be converted.
  520. ******************************************************************************/
  521.  
  522. status = CDFlib (SELECT_, CDF_, srcId,
  523.          GET_, CDF_NUMATTRS_, &numAttrs,
  524.          NULL_);
  525. if (! StatusHandler("SRC",status)) return FALSE;
  526.  
  527. for (attrNum = 0; attrNum < numAttrs; attrNum++) {
  528.    /***************************************************************************
  529.    * Inquire attribute in source CDF.
  530.    ***************************************************************************/
  531.  
  532.    status = CDFlib (SELECT_, CDF_, srcId,
  533.                  ATTR_, attrNum,
  534.             GET_, ATTR_NAME_, attrName,
  535.               ATTR_SCOPE_, &scope,
  536.               ATTR_MAXENTRY_, &maxEntry,
  537.             NULL_);
  538.    if (! StatusHandler("SRC",status)) return FALSE;
  539.  
  540.    /***************************************************************************
  541.    * If an assumed scope, check if the scope from the skeleton CDF can be used.
  542.    ***************************************************************************/
  543.  
  544.    if (scope == GLOBAL_SCOPE_ASSUMED || scope == VARIABLE_SCOPE_ASSUMED)
  545.      if (useSkeletonCDF) {
  546.        status = CDFlib (SELECT_, CDF_, sktId,
  547.                  ATTR_NAME_, attrName,
  548.             GET_, ATTR_SCOPE_, &sktScope,
  549.             NULL_);
  550.        if (status != NO_SUCH_ATTR) {
  551.      if (! StatusHandler("SKT",status)) return FALSE;
  552.      scope = sktScope;
  553.        }
  554.      }
  555.  
  556.    /***************************************************************************
  557.    * Create new attribute in destination CDF.
  558.    ***************************************************************************/
  559.  
  560.    status = CDFlib (SELECT_, CDF_, dstId,
  561.             CREATE_, ATTR_, attrName, scope, &ignoredNum,
  562.             NULL_);
  563.    if (! StatusHandler("DST",status)) return FALSE;
  564.  
  565.    /***************************************************************************
  566.    * Copy entries from source CDF to destination CDF.
  567.    ***************************************************************************/
  568.  
  569.    for (entryNum = 0; entryNum <= maxEntry; entryNum++) {
  570.       status = CDFlib (SELECT_, CDF_, srcId,
  571.                 ENTRY_, entryNum,
  572.                GET_, ENTRY_DATATYPE_, &dataType,
  573.                  ENTRY_NUMELEMS_, &numElements,
  574.                NULL_);
  575.       if (status != NO_SUCH_ENTRY) {
  576.     if (! StatusHandler("SRC",status)) return FALSE;
  577.  
  578.     MALLOC (value, ElemSize(dataType) * numElements);
  579.  
  580.     status = CDFlib (SELECT_, CDF_, srcId,
  581.              GET_, ENTRY_DATA_, value,
  582.              NULL_);
  583.     if (! StatusHandler("SRC",status)) return FALSE;
  584.  
  585.     status = CDFlib (SELECT_, CDF_, dstId,
  586.                   ENTRY_, entryNum,
  587.              PUT_, ENTRY_DATA_, dataType, numElements, value,
  588.              NULL_);
  589.     if (! StatusHandler("DST",status)) return FALSE;
  590.  
  591.     free (value);
  592.       }
  593.    }
  594. }
  595.  
  596. return TRUE;    /* Success. */
  597. }
  598.  
  599. /******************************************************************************
  600. * ConvertVariables.
  601. ******************************************************************************/
  602.  
  603. Boolean ConvertVariables (SRCmajority, DSTmajority)
  604. long SRCmajority;
  605. long DSTmajority;
  606. {
  607. CDFstatus status;
  608. long numVars;
  609. long varNum, ignoredNum;
  610. long dataType, numElements;
  611. long recVary, dimVarys[CDF_MAX_DIMS];
  612. char varName[CDF_VAR_NAME_LEN + 1];
  613. long NrecValues, NvalueBytes, NrecBytes;
  614. long recNum, maxRec;
  615. long NextendRecs;
  616. long dimN, i;
  617. void *buffer;
  618. void *bufferX;                   /* Buffer with switched majority. */
  619. void *bufferZ;
  620. void *value;
  621. void *fillValue;
  622. enum flipENUM {noFLIP_,ROWtoCOL_,COLtoROW_} flip;
  623. enum accTypeENUM {HYP,SEQ,SIN} accType;
  624.  
  625. static long HYPindices[CDF_MAX_DIMS] = {0,0,0,0,0,0,0,0,0,0};
  626. static long HYPcounts[CDF_MAX_DIMS];
  627. static long HYPintervals[CDF_MAX_DIMS] = {1,1,1,1,1,1,1,1,1,1};
  628.  
  629. static long SEQindices[CDF_MAX_DIMS] = {0,0,0,0,0,0,0,0,0,0};
  630.  
  631. static long SINcounts[CDF_MAX_DIMS];
  632. long indices[CDF_MAX_DIMS];
  633.  
  634. if (mLog) printf ("  (converting variables)\n");
  635.  
  636. /******************************************************************************
  637. * Inquire number of variables to be converted.
  638. ******************************************************************************/
  639.  
  640. status = CDFlib (SELECT_, CDF_, srcId,
  641.          GET_, CDF_NUMVARS_, &numVars,
  642.          NULL_);
  643. if (! StatusHandler("SRC",status)) return FALSE;
  644.  
  645. for (varNum = 0; varNum < numVars; varNum++) {
  646.    /***************************************************************************
  647.    * Inquire existing variable in source CDF.
  648.    ***************************************************************************/
  649.  
  650.    status = CDFlib (SELECT_, CDF_, srcId,
  651.                  VAR_, varNum,
  652.             GET_, VAR_NAME_, varName,
  653.               VAR_DATATYPE_, &dataType,
  654.               VAR_NUMELEMS_, &numElements,
  655.               VAR_RECVARY_, &recVary,
  656.               VAR_DIMVARYS_, dimVarys,
  657.               VAR_MAXREC_, &maxRec,
  658.               VAR_EXTENDRECS_, &NextendRecs,
  659.             NULL_);
  660.    if (! StatusHandler("SRC",status)) return FALSE;
  661.  
  662.    /***************************************************************************
  663.    * Creating new variable in destination CDF.
  664.    ***************************************************************************/
  665.  
  666.    status = CDFlib (SELECT_, CDF_, dstId,
  667.             CREATE_, VAR_, varName, dataType, numElements, recVary,
  668.                    dimVarys, &ignoredNum,
  669.             PUT_, VAR_EXTENDRECS_, NextendRecs,
  670.             NULL_);
  671.    if (! StatusHandler("DST",status)) return FALSE;
  672.  
  673.    /***************************************************************************
  674.    * Specify fill value for new variable (if it exists for old variable).
  675.    ***************************************************************************/
  676.  
  677.    NvalueBytes = numElements * ElemSize(dataType);
  678.    MALLOC (fillValue, NvalueBytes);
  679.  
  680.    status = CDFlib (SELECT_, CDF_, srcId,
  681.             GET_, VAR_FILLVALUE_, fillValue,
  682.             NULL_);
  683.    if (status != NO_FILLVALUE_SPECIFIED) {
  684.      if (! StatusHandler("SRC",status)) return FALSE;
  685.      status = CDFlib (SELECT_, CDF_, dstId,
  686.               PUT_, VAR_FILLVALUE_, fillValue,
  687.               NULL_);
  688.      if (! StatusHandler("DST",status)) return FALSE;
  689.    }
  690.  
  691.    free (fillValue);
  692.  
  693.    /***************************************************************************
  694.    * Calculate number of values/bytes per record.
  695.    ***************************************************************************/
  696.  
  697.    NrecValues = 1;
  698.  
  699.    if (numDims > 0)
  700.      for (dimN = 0; dimN < numDims; dimN++)
  701.         if (dimVarys[dimN]) NrecValues *= dimSizes[dimN];
  702.  
  703.    NrecBytes = NrecValues * NvalueBytes;
  704.  
  705.    /***************************************************************************
  706.    * Determine type of access.
  707.    ***************************************************************************/
  708.  
  709.    switch (numDims) {
  710.      case 0:
  711.     accType = SEQ;
  712.     MALLOC (value, NvalueBytes);
  713.     break;
  714.  
  715.      case 1:
  716. #if defined(__MSDOS__)
  717.     if (NrecBytes > (long) 65535) {
  718.       accType = SEQ;
  719.       MALLOC (value, NvalueBytes);
  720.     }
  721.     else {
  722. #endif
  723.       buffer = (void *) malloc (NrecBytes);
  724.       if (buffer == NULL) {
  725.         accType = SEQ;
  726.         MALLOC (value, NvalueBytes);
  727.       }
  728.       else {
  729.         accType = HYP;
  730.         flip = noFLIP_;
  731.       }
  732. #if defined(__MSDOS__)
  733.     }
  734. #endif
  735.  
  736.     break;
  737.  
  738.      default:
  739. #if defined(__MSDOS__)
  740.     if (NrecBytes > (long) 65535) {
  741.       if (SRCmajority == DSTmajority)
  742.         accType = SEQ;
  743.       else
  744.         accType = SIN;
  745.       MALLOC (value, NvalueBytes);
  746.     }
  747.     else {
  748. #endif
  749.       buffer = (void *) malloc (NrecBytes);
  750.       if (buffer != NULL)
  751.         if (SRCmajority != DSTmajority) {
  752.           bufferX = (void *) malloc (NrecBytes);
  753.           if (bufferX != NULL) {
  754.             accType = HYP;
  755.         if (SRCmajority == ROW_MAJOR)
  756.           flip = ROWtoCOL_;
  757.         else
  758.           flip = COLtoROW_;
  759.           }
  760.           else {
  761.             free (buffer);
  762.         accType = SIN;
  763.         MALLOC (value, NvalueBytes);
  764.           }
  765.         }
  766.         else {
  767.           accType = HYP;
  768.           flip = noFLIP_;
  769.         }
  770.       else {
  771.         if (SRCmajority == DSTmajority)
  772.           accType = SEQ;
  773.         else
  774.           accType = SIN;
  775.         MALLOC (value, NvalueBytes);
  776.       }
  777. #if defined(__MSDOS__)
  778.     }
  779. #endif
  780.    }
  781.  
  782.    /***************************************************************************
  783.    * Convert each variable record.
  784.    ***************************************************************************/
  785.  
  786.    switch (accType) {
  787.      case HYP:
  788.        /***********************************************************************
  789.        * Use hyper reads/writes.
  790.        ***********************************************************************/
  791.  
  792.        if (mLog) {
  793.      printf ("    (converting variable \"%s\" values with ", varName);
  794.      printf ("hyper reads/writes)\n");
  795.        }
  796.  
  797.        for (dimN = 0; dimN < numDims; dimN++)
  798.       if (dimVarys[dimN])
  799.         HYPcounts[dimN] = dimSizes[dimN];
  800.       else
  801.         HYPcounts[dimN] = 1;
  802.  
  803.        status = CDFlib (SELECT_, CDF_, srcId,
  804.                  CDF_RECCOUNT_, (long) 1,
  805.                  CDF_RECINTERVAL_, (long) 1,
  806.                  CDF_DIMINDICES_, HYPindices,
  807.                  CDF_DIMCOUNTS_, HYPcounts,
  808.                  CDF_DIMINTERVALS_, HYPintervals,
  809.             NULL_);
  810.        if (! StatusHandler("SRC",status)) return FALSE;
  811.  
  812.        status = CDFlib (SELECT_, CDF_, dstId,
  813.                  CDF_RECCOUNT_, (long) 1,
  814.                  CDF_RECINTERVAL_, (long) 1,
  815.                  CDF_DIMINDICES_, HYPindices,
  816.                  CDF_DIMCOUNTS_, HYPcounts,
  817.                  CDF_DIMINTERVALS_, HYPintervals,
  818.             NULL_);
  819.        if (! StatusHandler("DST",status)) return FALSE;
  820.  
  821.        for (recNum = 0; recNum <= maxRec; recNum++) {
  822.       status = CDFlib (SELECT_, CDF_, srcId,
  823.                     CDF_RECNUMBER_, recNum,
  824.                GET_, VAR_HYPERDATA_, buffer,
  825.                NULL_);
  826.           if (! StatusHandler("SRC",status)) return FALSE;
  827.  
  828.       switch (flip) {
  829.         case ROWtoCOL_:
  830.           ROWtoCOL (buffer, bufferX, numDims, HYPcounts, NvalueBytes);
  831.           bufferZ = bufferX;
  832.           break;
  833.         case COLtoROW_:
  834.           COLtoROW (buffer, bufferX, numDims, HYPcounts, NvalueBytes);
  835.           bufferZ = bufferX;
  836.           break;
  837.         case noFLIP_:
  838.           bufferZ = buffer;
  839.           break;
  840.       }
  841.  
  842.       status = CDFlib (SELECT_, CDF_, dstId,
  843.                     CDF_RECNUMBER_, recNum,
  844.                PUT_, VAR_HYPERDATA_, bufferZ,
  845.                NULL_);
  846.           if (! StatusHandler("DST",status)) return FALSE;
  847.        }
  848.  
  849.        free (buffer);
  850.        if (SRCmajority != DSTmajority) free (bufferX);
  851.  
  852.        break;
  853.  
  854.      case SEQ:
  855.        /***********************************************************************
  856.        * Use sequential reads/writes.
  857.        ***********************************************************************/
  858.  
  859.        if (mLog) {
  860.      printf ("    (converting variable \"%s\" values with ", varName);
  861.      printf ("sequential reads/writes)\n");
  862.        }
  863.  
  864.        status = CDFlib (SELECT_, CDF_, srcId,
  865.                  VAR_SEQPOS_, (long) 0, SEQindices,
  866.             NULL_);
  867.        if (! StatusHandler("SRC",status)) return FALSE;
  868.  
  869.        status = CDFlib (SELECT_, CDF_, dstId,
  870.                  VAR_SEQPOS_, (long) 0, SEQindices,
  871.             NULL_);
  872.        if (! StatusHandler("DST",status)) return FALSE;
  873.  
  874.        status = CDFlib (SELECT_, CDF_, srcId,
  875.             GET_, VAR_SEQDATA_, value,
  876.             NULL_);
  877.        while (status >= CDF_OK) {
  878.      status = CDFlib (SELECT_, CDF_, dstId,
  879.               PUT_, VAR_SEQDATA_, value,
  880.               NULL_);
  881.          if (! StatusHandler("DST",status)) return FALSE;
  882.  
  883.      status = CDFlib (SELECT_, CDF_, srcId,
  884.               GET_, VAR_SEQDATA_, value,
  885.               NULL_);
  886.        }
  887.        if (status != END_OF_VAR)
  888.      if (! StatusHandler("SRC",status)) return FALSE;
  889.  
  890.        free (value);
  891.        break;
  892.  
  893.      case SIN:
  894.        /***********************************************************************
  895.        * Use Single reads/writes.
  896.        ***********************************************************************/
  897.  
  898.        if (mLog) {
  899.      printf ("    (converting variable \"%s\" values with ", varName);
  900.      printf ("single reads/writes)\n");
  901.        }
  902.  
  903.        for (dimN = 0; dimN < numDims; dimN++)
  904.       if (dimVarys[dimN])
  905.         SINcounts[dimN] = dimSizes[dimN];
  906.       else
  907.         SINcounts[dimN] = 1;
  908.  
  909.        for (recNum = 0; recNum <= maxRec; recNum++) {
  910.       for (dimN = 0; dimN < numDims; dimN++) indices[dimN] = 0;
  911.  
  912.           status = CDFlib (SELECT_, CDF_, srcId,
  913.                     CDF_RECNUMBER_, recNum,
  914.                NULL_);
  915.           if (! StatusHandler("SRC",status)) return FALSE;
  916.  
  917.           status = CDFlib (SELECT_, CDF_, dstId,
  918.                     CDF_RECNUMBER_, recNum,
  919.                NULL_);
  920.           if (! StatusHandler("DST",status)) return FALSE;
  921.  
  922.       for (i = 0; i < NrecValues; i++) {
  923.          status = CDFlib (SELECT_, CDF_, srcId,
  924.                        CDF_DIMINDICES_, indices,
  925.                   GET_, VAR_DATA_, value,
  926.                   NULL_);
  927.              if (! StatusHandler("SRC",status)) return FALSE;
  928.  
  929.          status = CDFlib (SELECT_, CDF_, dstId,
  930.                        CDF_DIMINDICES_, indices,
  931.                   PUT_, VAR_DATA_, value,
  932.                   NULL_);
  933.              if (! StatusHandler("DST",status)) return FALSE;
  934.  
  935.          if (SRCmajority == ROW_MAJOR) {
  936.            INCRindicesROW (numDims, SINcounts, indices);
  937.          }
  938.          else {
  939.            INCRindicesCOL (numDims, SINcounts, indices);
  940.          }
  941.       }
  942.        }
  943.  
  944.        free (value);
  945.  
  946.        break;
  947.    }
  948. }
  949.  
  950. return TRUE;    /* Success. */
  951. }
  952.  
  953. /******************************************************************************
  954. * StatusHandler.
  955. ******************************************************************************/
  956.  
  957. Boolean StatusHandler (which, status)
  958. char *which;
  959. CDFstatus status;
  960. {
  961. char text[CDF_STATUSTEXT_LEN + 1];
  962.  
  963. if (status == CDF_OK) return TRUE;        /* Do nothing. */
  964.  
  965. CDFlib (SELECT_, CDF_STATUS_, status,
  966.     GET_, STATUS_TEXT_, text,
  967.     NULL_);
  968.  
  969. if (status < CDF_WARN) {
  970.   printf ("  ERROR, %s> %s\n", which, text);
  971.   return FALSE;
  972. }
  973. else {
  974.   if (mLog)
  975.     if (status < CDF_OK)
  976.       printf ("  WARNING, %s> %s\n", which, text);
  977.     else
  978.       printf ("  INFO, %s> %s\n", which, text);
  979.   return TRUE;
  980. }
  981. }
  982.